<?php
class Pagination {
/*
Người tạo	: Lại Ngọc Thái Châu - Team 0
Module 	: Pagination
Chức năng	: phan trang
 */
	public $url;
	public $total_page;
	public $cur_page;
	
	// ham khoi tao phan trang
  	public function init($_url,$_total_book,$_limit,$_cur_page){
  	     $_total_page = floor($_total_book/$_limit);
            if ($_total_book % $_limit != 0) $_total_page++;

            $this->url = $_url;
            $this->total_page = $_total_page;
            $this->cur_page = $_cur_page;

  	}
    // phan trang load page binh thuong
	public function render()
        {
            $html = "";
            if ($this->total_page > 1)
            {
                if ($this->cur_page < 4)
                    $html .= "<span class='disabled'><<</span>";
                else
                {
                    $html .= " <a href='" .$this->url .(($this->cur_page - 10 < 1) ? 1 : $this->cur_page - 10) ."'><<</a>";
                    $html .= "…";
                }

                if ($this->cur_page > 3 && $this->cur_page < $this->total_page - 2)
                {
                    for ($i = $this->cur_page - 2; $i <= $this->cur_page + 2; $i++)
                    {
                        if ($this->cur_page == $i)
                            $html .= " <span class='current'>".$i."</span>";
                        else
                            $html .= "<a href='". $this->url .$i. "'>" .$i. "</a>";
                    }
                }
                else if ($this->cur_page <= 3)
                {
                    for ($i = 1; $i <= ($this->total_page < 5 ? $this->total_page : 5); $i++)
                    {
                        if ($this->cur_page == $i)
                            $html .= " <span class='current'>" .$i. "</span>";
                        else
                            $html .= " <a href='" .$this->url .$i. "'>" .$i. "</a>";
                    }
                }
                else
                {
                    for ($i = $this->total_page - ($this->total_page < 5 ? 3 : 4); $i <= $this->total_page; $i++)
                    {
                        if ($this->cur_page == $i)
                            $html .= " <span class='current'>".$i."</span>";
                        else
                            $html .= " <a href='". $this->url.$i."'>".$i."</a>";
                    }
                }

                if ($this->cur_page > $this->total_page - 3)
                    $html .= "<span class='disabled'>>></span>";
                else
                {
                    $html .= " …";
                    $html .= "<a href='".$this->url.(($this->cur_page + 10 > $this->total_page) ? $this->total_page : $this->cur_page + 10)." '>>></a>";
                }
            }
            return $html;
        }
        
   // phan trang bang ajax
   public function renderJson()
        {
             
        	$html = "";
            if ($this->total_page > 1)
            {
                if ($this->cur_page < 4)
                    $html .= "<span class='disabled'><<</span>";
                else
                {
                    $html .= " <a href='javascript:;' onclick=jsonPage('".($this->url.(($this->cur_page - 10 < 1) ? 1 : $this->cur_page - 10))."')></a>";
                    $html .= "…";
                }
				
                if ($this->cur_page > 3 && $this->cur_page < $this->total_page - 2)
                {
                    for ($i = $this->cur_page - 2; $i <= $this->cur_page + 2; $i++)
                    {
                        if ($this->cur_page == $i)
                            $html .= " <span class='current'>".$i."</span>";
                        else
                            $html .= "<a href='javascript:;' onclick=jsonPage('". $this->url.$i ."')>".$i."<<</a>";
                    }
                }
                else if ($this->cur_page <= 3)
                {
                    for ($i = 1; $i <= ($this->total_page < 5 ? $this->total_page : 5); $i++)
                    {
                    	
                        if ($this->cur_page == $i)
                            $html .= " <span class='current'>".$i."</span>";
                        else
                            $html .= " <a href='javascript:;' onclick=jsonPage('".$this->url.$i."')>".$i."</a>";
                    }
                }
                else
                {
                    for ($i = $this->total_page - ($this->total_page < 5 ? 3 : 4); $i <= $this->total_page; $i++)
                    {
                        if ($this->cur_page == $i)
                            $html .= " <span class='current'>".$i."</span>";
                        else
                            $html .= " <a href='javascript:;' onclick=jsonPage('". $this->url.$i  ."')>".$i."</a>";
                    }
                }
				
                if ($this->cur_page > $this->total_page - 3)
                    $html .= "<span class='disabled'>>></span>";
                else
                {
                    $html .= " …";
                    $html .= "<a href='javascript:;' onclick=jsonPage('".$this->url.(($this->cur_page + 10 > $this->total_page) ? $this->total_page : $this->cur_page + 10)."')>>></a>";
                }
            }
           return $html;
        }
    }

